<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class notice extends CI_Controller{
	public function __construct(){
		parent::__construct();
		if(!isset($_SESSION["isadmin"]) || !$_SESSION["isadmin"]){
			header("location:/index.php/login");
		}
	}
	
	public function noticelist(){
            
            $url = "/index.php?c=notice&a=noticelist";
            $title = isset( $_GET['title']) ? $_GET['title'] : '';
            $data['title'] = $title;
            $where = "";
            if( $title ){
                $where  = " WHERE title like '%{$title}%' " ;
                $url.= "&title=".$title;
            }
            $this->config->load('pagination',true);
            $pagesize  = $this->config->item("per_page","pagination");
            $sposition  = isset($_GET['per_page']) && (int)$_GET['per_page'] ? (int)$_GET['per_page'] : 0;
            
            $sql = " SELECT  * FROM pk_notice $where ORDER BY ctime DESC  LIMIT $sposition ,$pagesize ";
            $this->load->model('NoticeModel');
            $data['noticeList'] = $this->NoticeModel->getAll($sql);
            
            $this->load->library('pagination');
            $config['base_url'] = $url;
            $config['total_rows'] = $this->NoticeModel->noticeCount($where);
            $this->pagination->initialize($config);
            $data['page'] = $this->pagination->create_links();
            
            $this->load->view("header");
            $this->load->view("notice/list",$data);
            $this->load->view("footer");
	}
	
	public function add(){
		$this->load->view("header");
		$this->load->view("notice/add",$data);
		$this->load->view("footer");
	}
        
        public function edit(){
            $noticeID = isset($_GET['noticeID']) && 0 < intval($_GET['noticeID']) ? intval($_GET['noticeID']) : '';
            if( !$noticeID ){
                mcmessage_error ('出错了,请重试', $_SESSION['HTTP_REFERER']);exit;
            }
            $data['noticeID'] = $noticeID;
            
            $this->load->model('NoticeModel');
            $sql = " SELECT * FROM pk_notice WHERE id = '{$noticeID}' ";
            $data['notice'] = $this->NoticeModel->getRow($sql);
            $this->load->view("header");
            $this->load->view("notice/add",$data);
            $this->load->view("footer");
	}
        
        public function save(){
            $info = $_GET;
            if( !empty($info['title']) && !empty($info['type']) && !empty($info['descripe'] )){
                $this->load->model('NoticeModel');
                if(isset($info['noticeID']) && 0<intval($info['noticeID']) ){
                    $sql = " UPDATE pk_notice SET `title` = '{$info['title']}',`descripe`='{$info['descripe']}',`type`='{$info['type']}' WHERE id ='{$info['noticeID']}' ";
                    $messg = "修改公告";
                }else{
                    $time = time();
                    $sql =" INSERT pk_notice SET `title` = '{$info['title']}',`descripe`='{$info['descripe']}',`type`='{$info['type']}',`adminID`='{$_SESSION['adminid']}',`adminName`='{$_SESSION['adminname']}',`ctime`='{$time}'";
                    $messg = "添加公告";
                }
                if( $this->NoticeModel->updateState($sql) ){
                    mcmessage_error(  $messg.'成功', '/index.php?c=notice&a=noticelist' );
                }else{
                    mcmessage_error( $messg."失败" ,  '/index.php?c=notice&a=noticelist' );
                }
            
            }else{
                mcmessage_error("您有未填项！", $_SERVER['HTTP_REFERER']);
            }
        }
        
        public function del(){
            $noticeID = isset($_GET['noticeID']) && 0 < intval($_GET['noticeID']) ? intval($_GET['noticeID']) : '';
            if( !$noticeID ){
                mcmessage_error ('出错了,请重试', $_SESSION['HTTP_REFERER']);exit;
            }
            
            $sql = " UPDATE pk_notice SET isDel = 1 WHERE id = '$noticeID' ";
            $this->load->model('NoticeModel');
            if( $this->NoticeModel->updateState($sql) ){
                 mcmessage_error(  '删除成功', $_SERVER['HTTP_REFERER'] );
            }else{
                 mcmessage_error( "删除失败" , $_SERVER['HTTP_REFERER'] );
            }
        }
}